import 'package:chewie/chewie.dart';
import 'package:flutter/material.dart';
import 'package:font_awesome_flutter/font_awesome_flutter.dart';
import '../../widgets/userSelect.dart';
import '../helper/routeHelper.dart';
import '../helper/Helper.dart';
import '../helper/cacheHelper.dart';
import '../helper/global_config.dart';

import 'dialogWidget.dart';
import 'signatureCard2.dart';

class InputWidget {
  static TextStyle subTxtStyle =
      TextStyle(fontSize: 13.0, color: Colors.black54);
  static TextStyle subTxtStyle2 =
      TextStyle(fontSize: 14.0, color: Colors.black54);

  static Widget getDivider2(double h1) {
    return SizedBox(
      width: 0.5,
      height: h1,
      child: DecoratedBox(
        decoration: BoxDecoration(color: Colors.grey),
      ),
    );
  }

  static Widget getDivider() {
    return Divider(
      height: 1.0,
    );
  }

  static Widget getInput2(String title, TextEditingController c1) {
    if (title == null) title = "";
    return ListTile(
        title: Text(
          title,
          //  style: TextStyle(fontWeight: FontWeight.bold),
        ),
        subtitle: TextField(
          maxLines: null,
          controller: c1,
          style: subTxtStyle2,
        ));
  }

  static Widget getUserSelect(
      String title, String auth, UserSelectedCallback selected) {
    if (title == null) title = "";
    return ListTile(
      title: Text(
        title,
        //  style: TextStyle(fontWeight: FontWeight.bold),
      ),
      trailing: UserSelectWidget(selected, auth),
    );
  }

  static Widget getInfo(String title, String value,
      {GestureLongPressCallback? onTap, String subtitle = ""}) {
    if (value == null) value = "";
    if (title == null) title = "";
    // if(onTap!=null) value=value+" >";
    return ListTile(
      title: Text(
        title,
        // style: TextStyle(fontWeight: FontWeight.bold),
      ),
      trailing: Text(value),
      subtitle: subtitle == "" ? null : Text(subtitle),
      onTap: onTap,
    );
  }

  static Widget getInfo2(String title, String value,
      {GestureLongPressCallback? onTap}) {
    if (value == null) value = "";

    return ListTile(
        title: Text(
          title,
          // style: TextStyle(fontWeight: FontWeight.bold),
        ),
        onTap: onTap,
        subtitle: Text(value, style: TextStyle(fontSize: 14.0, height: 1.6)));
  }

  static Widget getGreyInfo2(String title, String value,
      {GestureLongPressCallback? onTap}) {
    if (value == null) value = "";
    if (title == null) title = "";
    // if(onTap!=null) value=value+" >";
    return TextButton(
      child: Container(
        height: 36.0,
        // color: Colors.red,
        child: Row(
          children: [
            Text(
              title,
              style: subTxtStyle,
            ),
            Text(
              value,
              style: subTxtStyle,
            ),
          ],
          mainAxisAlignment: MainAxisAlignment.spaceBetween,
          crossAxisAlignment: CrossAxisAlignment.center,
        ),
      ),
      onPressed: onTap,
    );
  }

  static Widget getGreyInfo(String title, String value,
      {GestureLongPressCallback? onTap}) {
    if (value == null) value = "";
    if (title == null) title = "";
    // if(onTap!=null) value=value+" >";
    return Container(
        height: 36.0,
        child: ListTile(
          // contentPadding: EdgeInsets.only(bottom:12.0),
          title: Text(
            title,
            style: subTxtStyle,
          ),
          trailing: Text(
            value,
            style: subTxtStyle,
          ),
          onTap: onTap,
        ));
  }

  static Widget getShowInfo(String title, String value, Color vColor,
      {GestureLongPressCallback? onTap}) {
    if (value == null) value = "";
    if (title == null) title = "";
    // if(onTap!=null) value=value+" >";
    return TextButton(
      child: Container(
          margin: EdgeInsets.only(top: 8.0, bottom: 8.0),
          child: Column(
              mainAxisAlignment: MainAxisAlignment.spaceEvenly,
              children: [
                Center(
                  child: Text(
                    title,
                    maxLines: 1,
                    style: TextStyle(fontSize: 14.0, color: Colors.black87),
                  ),
                ),
                Center(
                  child: Text(
                    value,
                    maxLines: 1,
                    style: TextStyle(
                        fontSize: 16.0,
                        color: vColor,
                        fontWeight: FontWeight.bold),
                  ),
                ),
              ])),
      onPressed: onTap,
    );
  }

  static Widget getShowInfo2(String title, Widget child, Color vColor,
      {GestureLongPressCallback? onTap}) {
    if (title == null) title = "";
    // if(onTap!=null) value=value+" >";
    return TextButton(
      child: Container(
          margin: EdgeInsets.only(top: 8.0, bottom: 8.0),
          child: Column(
              mainAxisAlignment: MainAxisAlignment.spaceEvenly,
              children: [
                Center(
                  child: Text(
                    title,
                    maxLines: 1,
                    style: TextStyle(fontSize: 14.0, color: Colors.black87),
                  ),
                ),
                Center(
                  child: child,
                ),
              ])),
      onPressed: onTap,
    );
  }

  static Widget getShowRow(List<Widget> cL, bool ifDivide) {
    var list1 = <Widget>[];
    for (var c1 in cL) {
      list1.add(Expanded(flex: 1, child: c1));
      if (ifDivide == true) {
        list1.add(getDivider2(24));
      } else {
        list1.add(getDivider2(0));
      }
    }
    list1.removeLast();

    return Row(children: list1);
  }

  static Widget getLocationTile(BuildContext context, String title,
      String content, double lat, double lng) {
    return getInfo(title, content, onTap: () {
      //RouteHelper.MyMapPage(context, lat, lng);
    });
  }

  static Widget getSelect(String title, List<String> values, String index,
      {onChanged? onChanged}) {
    if (values.length == 0) return Container();

    if (index == null) index = "";
    if (title == null) title = "";
    if (index == "") index = values[0];
    if (!values.contains(index)) index = values[0];
    List<DropdownMenuItem> items = [];
    for (var a in values) {
      var item1 =
          new DropdownMenuItem(key: Key(a), value: a, child: new Text(a));
      items.add(item1);
    }

    return ListTile(
        title: Text(
          title,
          //  style: TextStyle(fontWeight: FontWeight.bold),
        ),
        trailing: Container(
            width: 140,
            child: DropdownButton(
              value: index,
              items: items,
              onChanged: onChanged,
            )));
  }

  static Widget getChecked(String title, List<String> values, String checks,
      {onChanged? onChanged, Color col = Colors.black87}) {
    if (values.length == 0) return Container();
    if (title == null) title = "";

    List<Widget> items = <Widget>[];
    for (var a in values) {
      var item1 = Row(children: [
        Text(a),
        Checkbox(
            value: checks.contains(a), activeColor: col, onChanged: onChanged)
      ]);
      items.add(item1);
    }

    return ListTile(
        title: Text(
          title,
          //  style: TextStyle(fontWeight: FontWeight.bold),
        ),
        subtitle: Wrap(
          children: items,
          direction: Axis.horizontal,
        ));
  }

  static Widget getSelect2(List<String> values, String index,
      {onChanged? onChanged, Color col = Colors.black87}) {
    if (values.length == 0) return Container();
    if (index == null) index = "";
    if (index == "") index = values[0];
    if (!values.contains(index)) index = values[0];

    List<DropdownMenuItem> items = [];
    for (var a in values) {
      var item1 = new DropdownMenuItem(
          key: Key(a),
          value: a,
          child: Center(
              child: Text(
            a,
            textAlign: TextAlign.center,
            textScaleFactor: 1.0,
            style: TextStyle(color: col),
          )));
      items.add(item1);
    }

    return Container(
        width: 140,
        child: DropdownButton(
          value: index,
          items: items,
          onChanged: onChanged,
        ));
  }

  static Widget getInput(String title, TextEditingController c1,
      {double w1 = 120}) {
    return ListTile(
        title: Text(
          title,
          // style: TextStyle(fontWeight: FontWeight.bold),
        ),
        trailing: Container(
            width: w1,
            child: TextField(
              maxLines: 1,
              controller: c1,
              textAlign: TextAlign.center,
            )));
  }

  static Widget getSearchButton(
      TextEditingController c1, GestureTapCallback onTap) {
    return ListTile(
        title: Container(
            // width: 240,
            child: TextField(
          maxLines: 1,
          controller: c1,
          textAlign: TextAlign.center,
        )),
        trailing: IconButton(
          // child: Icon(Icons.crop_square,size:32.0,color: Colors.blue,),
          icon: Icon(
            Icons.search,
            color: Colors.blue,
            size: 32.0,
          ),
          onPressed: onTap,
        ));
  }

  static Widget getButton3(
      String title, TextEditingController c1, GestureTapCallback onTap) {
    return Row(children: [
      Expanded(
          flex: 3,
          child: ListTile(
            title: Text(
              title,
              // style: TextStyle(fontWeight: FontWeight.bold),
            ),
            trailing: Container(
                width: 120,
                child: TextField(
                  maxLines: 1,
                  controller: c1,
                  textAlign: TextAlign.center,
                )),
          )),
      Expanded(
        child: TextButton(
          // child: Icon(Icons.crop_square,size:32.0,color: Colors.blue,),
          child: Text("计算",
              style:
                  TextStyle(color: Colors.blue, fontWeight: FontWeight.bold)),
          onPressed: onTap,
        ),
      )
    ]);
  }

  static Widget getButton4(
      String title, TextEditingController c1, GestureTapCallback onTap) {
    var v1 = Center(child: Text(title));
    var v2 = Container(
        // width: 120,
        child: TextField(
      maxLines: 1,
      controller: c1,
      textAlign: TextAlign.center,
    ));
    var v3 = ElevatedButton(
      // child: Icon(Icons.crop_square,size:32.0,color: Colors.blue,),
      child: Text("计算",
          style: TextStyle(color: Colors.blue, fontWeight: FontWeight.bold)),
      onPressed: onTap,
    );
    return Container(
        margin: EdgeInsets.only(top: 8.0, bottom: 8.0),
        child: getShowRow([v1, v2, v3], false));
  }

  static Widget getInput3(String title, TextEditingController c1) {
    return ListTile(
        title: Text(
          title,
          // style: TextStyle(fontWeight: FontWeight.bold),
        ),
        subtitle: TextField(
          maxLines: 1,
          controller: c1,
        ));
  }

  static Widget getButton(String title, GestureTapCallback onTap) {
    return TextButton(
      // color: Colors.black12,
      child: Center(
          child: Text(title,
              style:
                  TextStyle(color: Colors.blue, fontWeight: FontWeight.bold))),
      onPressed: onTap,
    );
  }

  static Widget getButton2(String title, GestureTapCallback onTap) {
    return ListTile(
      title: Center(
          child: Text(title,
              style:
                  TextStyle(color: Colors.blue, fontWeight: FontWeight.bold))),
      onTap: onTap,
    );
  }

  static Widget getQMTile(bool ifOk, String content, String imgId) {
    if (content == null) content = "";

    if (!ifOk)
      return ListTile(
          title: Text(
        "未处理",
        style: TextStyle(color: Colors.black54, height: 2.0),
      ));
    return ListTile(
      title: Text(
        content,
        style: TextStyle(color: Colors.black54, height: 2.0),
      ),
      trailing: Helper.Base64ToImage(imgId),
    );
  }

  static Widget getQMTile2(String title, bool ifOk, String imgId, String tm) {
    if (!ifOk)
      return ListTile(
        title: Text(
          title,
        ),
        trailing: Text("未处理", style: subTxtStyle),
      );
    return Container(
        margin: const EdgeInsets.only(top: 3.0, bottom: 3.0),
        child: ListTile(
          title: Text(
            title,
          ),
          trailing: Helper.Base64ToImage(imgId),
          subtitle: Text(Helper.DateTimeFormat(tm)),
        ));
  }

  static Widget getQMTile4(BuildContext context, String title, bool ifOk,
      String userID, String imgId, String tm) {
    if (!ifOk) {
      var u1 = CacheHelper.GetUser(userID);

      return ListTile(
        title: Text(
          title,
        ),
        subtitle: Text(u1.Name),
        onTap: () {
          if (u1.Phone == null) return;

          showDialog(
              context: context,
              builder: (BuildContext context) {
                return DialogWidget.getDialog(
                    context, "拨打电话", "点击确定将拨打电话给" + u1.Name, () {
                  Helper.CallPhone(u1.Phone);
                  ;
                });
              });
        },
        trailing: Text("未处理", style: subTxtStyle),
      );
    }
    return Container(
        margin: const EdgeInsets.only(top: 3.0, bottom: 3.0),
        child: ListTile(
          title: Text(
            title,
          ),
          trailing: Helper.Base64ToImage(imgId),
          subtitle: Text(tm),
        ));
  }

  static Widget getQMTile3(String title, String imgId) {
    bool ifOk = true;

    if (imgId == null) ifOk = false;
    if (imgId == "") ifOk = false;
    if (title == null) title = "";

    if (!ifOk) return getInfo(title, "未签名");
    return ListTile(
      title: Text(
        title,
        // style: TextStyle(color: Colors.black54, height: 2.0),
      ),
      trailing: Container(
        child: Helper.Base64ToImage(imgId),
        width: 84.0,
        height: 42.0,
      ),
    );
  }

  static Widget getQMInput(
      BuildContext ctx, String title, String imgId, onStrChanged qmOver) {
    bool ifOk = true;

    if (imgId == null) ifOk = false;

    if (imgId == "") ifOk = false;

    if (title == null) title = "";

    if (!ifOk)
      return ListTile(
        title: Text(
          title,
          // style: TextStyle(color: Colors.black54, height: 2.0),
        ),
        trailing: Text("点击签字", style: TextStyle(color: Colors.blue)),
        onTap: () {
          showModalBottomSheet(
            context: ctx,
            builder: (BuildContext context) {
              return SignatureCard2Page(qmOver, title);
            },
          );
        },
      );

    return getQMTile3(title, imgId);
  }

  static FloatingActionButton getQMButton(
      BuildContext ctx, String title, onStrChanged qmOver) {
    return FloatingActionButton(
        child: Icon(Icons.check),
        onPressed: () {
          showModalBottomSheet(
            context: ctx,
            builder: (BuildContext context) {
              return SignatureCard2Page(qmOver, title);
            },
          );
        });
  }

  static Widget getUserTile(BuildContext context, String title, String userId) {
    var u1 = CacheHelper.GetUser(userId);
    if (u1 == null) return Container();

    return ListTile(
      title: Text(title),
      trailing: Text(u1.Name),
      onTap: () {
        if (u1.Phone == null) return;

        showDialog(
            context: context,
            builder: (BuildContext context) {
              return DialogWidget.getDialog(
                  context, "拨打电话", "点击确定将拨打电话给" + u1.Name, () {
                Helper.CallPhone(u1.Phone);
                ;
              });
            });
      },
    );
  }

  static Widget getUserTile2(
      BuildContext context, String title, String name, String phone) {
    if (name == null) name = "";
    if (phone == null) phone = "";

    return ListTile(
      title: Text(title),
      trailing: Text(name),
      onTap: () {
        if (phone == null) return;

        showDialog(
            context: context,
            builder: (BuildContext context) {
              return DialogWidget.getDialog(
                  context, "拨打电话", "点击确定将拨打电话给" + name, () {
                Helper.CallPhone(phone);

                ;
              });
            });
      },
    );
  }

  static Widget inCard4(Widget s1, {String? key, double margin1 = 6.0}) {
    if (key == null) key = Helper.uuid.v1();

    return Container(
      key: Key(key),
      color: GlobalConfig.cardBackgroundColor,
      margin: EdgeInsets.only(top: margin1),
      child: s1,
    );
  }

  static Widget inCard(Widget s1, {String? key}) {
    if (key == null) key = Helper.uuid.v1();

    return Card(
      key: Key(key),
      color: GlobalConfig.cardBackgroundColor,
      margin: const EdgeInsets.all(6.0),
      child: s1,
    );
  }

  static Widget inCard2(List<Widget> s1, {String? key}) {
    if (key == null) key = Helper.uuid.v1();

    return Card(
        key: Key(key),
        color: GlobalConfig.cardBackgroundColor,
        margin: const EdgeInsets.all(6.0),
        child: Column(
          children: s1,
        ));
  }

  static Widget inCard3(List<Widget> s1,
      {String? key, Color? col1, double margin1 = 6.0}) {
    if (key == null) key = Helper.uuid.v1();
    if (col1 == null) col1 = GlobalConfig.cardBackgroundColor;
    return Container(
        key: Key(key),
        color: col1,
        margin: EdgeInsets.only(top: margin1),
        child: Column(
          children: s1,
        ));
  }

  static Widget userCircle() {
    return Container(
      child: new CircleAvatar(
        backgroundColor: Colors.blue,
        radius: 8.0,
      ),
      margin: EdgeInsets.only(right: 6.0),
    );
  }

  static Widget addAppbar(String title, Widget c1) {
    return Scaffold(
      appBar: AppBar(
        title: Text(title),
      ),
      body: c1,
    );
  }
}
